home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 050 / madtrb17.arc / TREK.P < prev    next >
Encoding:
Text File  |  1985-09-08  |  15.5 KB  |  657 lines

  1. {$B-}
  2. {$I-}
  3. {$C-}
  4. {$K-}
  5.  
  6. (*
  7. ::::::::::
  8. TREK.TEXT
  9. ::::::::::
  10. *)
  11.  
  12. (*$I-*)
  13. (*$R-*)
  14. program startrek;
  15. label 10;
  16.  
  17. const
  18.     skill = 2;
  19.     maxscroll= 40;
  20.     maxlines = 22;
  21.     opefficiency = 75;
  22.     maxuni = 32;         (* limits size of array *)
  23.     solarlim = 22;       (* limits placement of characters *)
  24.     pi = 3.14159;
  25.     maxlist = 'n';
  26.  
  27. type
  28.     space = packed record
  29.     pts, ch: char;
  30.     strength: integer
  31.     end;
  32.     attack = (fired, chanced);
  33.     what   = (pass, go);
  34.     system = (computer, phasers, longscan, shortscan, torpedos, warp, impulse);
  35.     unirange = -maxuni..maxuni;
  36.     galaxy  = array [unirange,unirange] of space;
  37.     string80 = string[80];
  38.  
  39. var
  40.     msginfo:  array [1..7] of integer;
  41.     universe:  galaxy;
  42.     systems:  array [system] of integer;
  43.     list: array ['a'..'n'] of space;
  44.     names: array[0..18] of string80;
  45.     nomove, seeall, longer, confuse, syshields,
  46.     points, allshields, maxpower, totalpower, currx, curry, level, nmbrbases,
  47.     highest, totalkling, nmbrkling, nmbrtorps,
  48.     shields:  integer;
  49.     currlst: char;
  50.     xdock, ydock:  unirange;
  51.     captain, str, condition:  string80;
  52.     stardate, deadline, direction, seed:  real;
  53.     partdone, alldone, return, babble, baseattacked, hasone:  boolean;
  54.     paddingforsave : packed array[0..73] of char;        { 37 blocks }
  55.  
  56.     g:  file;
  57.     linesprinted, scrollinfo : integer;
  58.     restored: boolean;
  59.  
  60.     regs : record
  61.        ax,bx,cx,dx,bp,si,di,ds,es,flags : integer;
  62.        end;
  63.     screen : packed array[0..23,0..79] of char;
  64.     blanks : packed array[0..90] of char;
  65.     more : string80;
  66.     viscursor : boolean;
  67.  
  68. function readreal : real; forward;
  69.  
  70. function readint : integer; forward;
  71.  
  72. procedure addln(str: string80); forward;
  73.  
  74. procedure addscroll(str:string80); forward;
  75.  
  76. procedure clearscroll; forward;
  77.  
  78. procedure clrmesg; forward;
  79.  
  80. procedure condcheck(x,y: integer; var red : boolean);  forward;
  81.  
  82. procedure disable (shot: integer); forward;
  83.  
  84. procedure initscroll; forward;
  85.  
  86. procedure mesg( line:integer; st:string80); forward;
  87.  
  88. procedure moveround(var initlx,initly: integer); forward;
  89.  
  90. procedure numstr(realnum:real; leng, decimal: integer); forward;
  91.  
  92. function ok(checkx, checky: integer): boolean; forward;
  93.  
  94. function rand(lolim, hilim: integer): integer; forward;
  95.  
  96. procedure sector(x,y: integer; var result : integer); forward;
  97.  
  98. procedure scroll; forward;
  99.  
  100. procedure scrollup(up,down,left,right: integer); forward;
  101.  
  102. procedure short(x,y: integer); forward;
  103.  
  104. procedure printch(x,y : integer; ch : char); forward;
  105.  
  106. procedure togglecursor; forward;
  107.  
  108.  
  109. {$I TREK_INI.P }
  110.  
  111. {$I TREK_OBJ.P }
  112.  
  113. {$I TREK_PLY.P }
  114.  
  115. { these are global procedures }
  116.  
  117. function pwroften(n:integer):integer;
  118. begin
  119. case n of
  120.     0 : pwroften := 1;
  121.     1 : pwroften := 10;
  122.     2 : pwroften := 100;
  123.     3 : pwroften := 1000;
  124.     4 : pwroften := 10000;
  125.     end;
  126. end;
  127.  
  128. function readreal {: real};
  129. const nodot = 42;
  130. var inc,dot,i,divisor,number,count,sign : integer;
  131.     ch : char;
  132.     buffer : char;
  133. begin
  134.     gotoxy(57,15); write('     '); gotoxy(57,15);
  135.     togglecursor;
  136.     dot := nodot; count := 0; sign := 1; number := 0;
  137.     repeat
  138.     read(kbd,buffer);
  139.     ch := buffer;
  140.     if ch = chr(8) then
  141.         if count > 0 then begin
  142.         if count = dot then dot := nodot
  143.         else number := number div 10;
  144.         count := count - 1;
  145.         gotoxy(wherex-1, wherey); write(' ');
  146.         gotoxy(wherex-1, wherey);
  147.         end
  148.         else if sign = -1 then begin
  149.         sign := 1;
  150.         gotoxy(wherex-1, wherey); write(' ');
  151.         gotoxy(wherex-1, wherey);
  152.         end
  153.         else begin
  154.         sound(480); delay(300); sound(1320); delay(200); nosound;
  155.         end
  156.     else if (ch = '-') and (count = 0) and (sign = 1) then begin
  157.         sign := -1; write(buffer)
  158.     end
  159.     else if (count <  5) then
  160.         if (ch = '.') and (dot = nodot) then begin
  161.         count := count + 1;
  162.         dot := count;
  163.         write(buffer)
  164.         end
  165.         else if ch in ['0'..'9'] then begin
  166.         inc := ord(ch) - ord('0');
  167.         if maxint - (number * 10) >= inc then begin
  168.             count := count + 1;
  169.             number := number * 10 + inc;
  170.             write(buffer)
  171.         end
  172.         end
  173.     until ch = chr(13);
  174.     if (dot = nodot) or (count - dot = 0) then
  175.     divisor := 1
  176.     else
  177.     divisor := trunc(pwroften(count - dot));
  178.     readreal := (number/divisor) * sign;
  179.     togglecursor
  180. end;
  181.  
  182.  
  183. function readint {: integer};
  184. var inc,number,count,sign : integer;
  185.     ch : char;
  186.     buffer : char;
  187. begin
  188.     gotoxy(57,14); write('      '); gotoxy(57,15);
  189.     togglecursor;
  190.     count := 0; sign := 1; number := 0;
  191.     repeat
  192.     read(kbd,buffer);
  193.     ch := buffer;
  194.     if ch = chr(8) then
  195.         if count > 0 then begin
  196.         number := number div 10;
  197.         count := count - 1;
  198.         gotoxy(wherex-1, wherey); write(' ');
  199.         gotoxy(wherex-1, wherey);
  200.         end
  201.         else if sign = -1 then begin
  202.         sign := 1;
  203.         gotoxy(wherex-1, wherey); write(' ');
  204.         gotoxy(wherex-1, wherey);
  205.         end
  206.         else begin
  207.         sound(480); delay(300); sound(1320); delay(200); nosound;
  208.         end
  209.     else if (ch = '-') and (count = 0) and (sign = 1) then begin
  210.         sign := -1; write(buffer)
  211.     end
  212.     else if (count < 5) and (ch in ['0'..'9']) then begin
  213.         inc := ord(ch) - ord('0');
  214.         if maxint - (number * 10) >= inc then begin
  215.         count := count + 1;
  216.         number := number * 10 + inc;
  217.         write(buffer)
  218.         end
  219.     end
  220.     until ch = chr(13);
  221.     readint := number * sign;
  222.     togglecursor
  223. end;
  224.  
  225. procedure addln{str: string};
  226. var
  227.    oldscroll,
  228.    x,l    : integer;
  229. begin
  230.      oldscroll := scrollinfo;
  231.      l := length(str);
  232.      scrollinfo := scrollinfo + l;
  233.      if scrollinfo > maxscroll then begin
  234.      l := l - scrollinfo + maxscroll;
  235.      scrollinfo := maxscroll
  236.      end;
  237.      gotoxy(oldscroll+1,linesprinted+1);
  238.      str := str + '    ';
  239.      move(str[1],screen[linesprinted,oldscroll],l);
  240.      for x := 1 to l do write(str[x]);
  241. end;
  242.  
  243. procedure clearscroll;
  244. var i : integer;
  245. begin
  246.      for i := 0 to linesprinted do begin
  247.      gotoxy(1,i+1);
  248.      write('                                         ');
  249.      move(blanks,screen[i],maxscroll+1)
  250.      end;
  251.      scrollinfo:= 0;
  252.      linesprinted:= 0;
  253. end;
  254.  
  255. procedure scroll;
  256. var
  257.     i: integer;
  258.     temp: char;
  259. begin
  260.     if linesprinted = maxlines then begin
  261.     gotoxy(1,24);
  262.     write(more);
  263.     read(kbd,temp);
  264.     while not (temp in [' ',chr(4),'d',chr(13)]) do begin
  265.         write(chr(7));
  266.         read(kbd,temp);
  267.     end;
  268.     gotoxy(1,24);
  269.     write('            ');
  270.     if temp = chr(13) then begin
  271.         linesprinted:= 21;
  272.         scrollup(0,maxlines,0,maxscroll)
  273.     end
  274.     else if temp = ' ' then
  275.        clearscroll
  276.     else begin
  277.         for i := maxlines downto 12 do
  278.         scrollup(0,i,0,maxscroll);
  279.         linesprinted:= 11;
  280.     end
  281.     end;
  282.     linesprinted:= linesprinted + 1;
  283.     scrollinfo:= 0;
  284. end;
  285.  
  286. procedure addscroll{str:string};
  287. begin
  288.      addln(str);
  289.      scroll
  290. end;
  291.  
  292. procedure initscroll;
  293. var i : integer;
  294. begin
  295.      for i := 0 to maxlines do
  296.      move(blanks,screen[i],maxscroll);
  297.      scrollinfo:= 0;
  298.      linesprinted:= 0;
  299. end;
  300.  
  301. procedure numstr{realnum:real; leng, decimal: integer};
  302. var
  303.     count, i, int: integer;
  304.     sample: string80;
  305. begin
  306.     sample:= '0123456789.';
  307.     str:= '';
  308.     for i:= 1 to decimal do
  309.     realnum:= realnum * 10;
  310.     int:= round(realnum);
  311.     if decimal <> 0 then leng:= leng - 1;
  312.     for i:= 1 to leng do begin
  313.     str:= concat(copy(sample,abs(int mod 10) + 1,1),str);
  314.     if i = decimal then str := concat(copy(sample,11,1),str);
  315.     int:= int div 10
  316.     end;
  317.     count:= 1;
  318.     while (str[count] = '0') and (length(str) > count) do  begin
  319.     str[count]:= ' ';
  320.     count:= count + 1
  321.     end;
  322.     if realnum < 0 then
  323.     str[1]:= '-'
  324. end;
  325.  
  326. procedure mesg{line:integer; st:string};
  327. var
  328.     i: integer;
  329. begin
  330.     for i:= 1 to length(st) do
  331.     printch(52+i,line+15,st[i]);
  332.     msginfo[line]:= length(st)
  333. end;
  334.  
  335. procedure clrmesg;
  336. var
  337.     i, j: integer;
  338. begin
  339.     for i:= 1 to 7 do begin
  340.     for j:= 1 to msginfo[i] do
  341.         printch(52+j,i+15,' ');
  342.     msginfo[i]:= 0
  343.     end;
  344. end;
  345.  
  346. function rand {lolim, hilim: integer): integer};
  347. begin
  348.     seed:= seed*27.1368+31.468;
  349.     seed:=seed-trunc(seed);
  350.     if lolim > hilim then
  351.     rand:= lolim
  352.     else
  353.     rand:= trunc(seed*(hilim-lolim+1)+lolim)
  354. end;
  355.  
  356. function ok{checkx, checky: integer): boolean};
  357. begin
  358.     ok := (abs(checkx)<=maxuni) and (abs(checky)<=maxuni)
  359. end;
  360.  
  361. procedure disable {shot: integer};
  362. var
  363.     origeffect, effected: system;
  364. begin
  365.     if syshields < 0 then  begin
  366.        case rand(1,7) of 
  367.         1:  effected:= longscan;
  368.         2:  effected:= shortscan;
  369.         3:  effected:= phasers;
  370.         4:  effected:= torpedos;
  371.         5:  effected:= computer;
  372.         6:  effected:= warp;
  373.         7:  effected:= impulse
  374.     end;
  375.     origeffect:= effected;
  376.     repeat
  377.         if systems[effected] = 0 then
  378.         if effected <> impulse then
  379.             effected:= succ(effected)
  380.         else
  381.             effected:= longscan
  382.     until (systems[effected] <> 0) or (effected = origeffect);
  383.     systems[effected]:= systems[effected] - round(shot/6);
  384.     if systems[effected] < 0 then systems[effected]:= 0
  385.     end
  386. end;
  387.  
  388. procedure moveround{var initlx,initly: integer};
  389. label 10;
  390. var
  391.    x,y,counter:  integer;
  392. begin
  393.     for counter:=0 to maxuni do
  394.     for x:= -counter to counter do
  395.         for y:= -counter to counter do
  396.         if ok(x+initlx,y+initly) then
  397.             if universe[initlx+x, initly+y].ch=' ' then begin
  398.             initlx:=x+initlx;
  399.             initly:=y+initly;
  400.             goto 10
  401.             end;
  402. 10: end;
  403.  
  404. procedure togglecursor;
  405. begin
  406.     viscursor := not viscursor;
  407. end;
  408.  
  409. { .PROC TREKINTRINSICS recoded from terak assembly }
  410.  
  411. function onscreen (x,y : integer) : char;
  412. begin
  413.     onscreen := screen[y,x]
  414. end;
  415.  
  416. procedure printch {x,y : integer; ch : char};
  417. begin
  418.     gotoxy(x+1,y+1); write(ch);
  419.     screen[y,x] := ch;
  420. end;
  421.  
  422. procedure short {x,y : integer};
  423. var
  424.     lim2,nx,ny,xscrn,yscrn : integer;
  425.     ch : char;
  426.     subscreen : packed array[0..10,0..20] of char;
  427. begin
  428.     lim2 := maxuni + 8;
  429.     xscrn := 0;
  430.     for nx := x - 10 to x + 10 do begin
  431.     yscrn := 10;
  432.     for ny := y - 5 to y + 5 do begin
  433.         if (yscrn = 5) and (xscrn = 10) then
  434.         ch := '@'
  435.         else if (abs(nx) > lim2) or (abs(ny) > lim2) then
  436.         ch := '$'
  437.         else if (abs(nx) > maxuni) or (abs(ny) > maxuni) then
  438.         ch := ' '
  439.         else begin
  440.         ch := universe[nx,ny].ch;
  441.         if ch = 'A' then begin if seeall < 0 then
  442.             ch := universe[nx,ny].pts end
  443.         else if ch = 'R' then begin
  444.             if seeall < 0 then
  445.             ch := ' '
  446.         end
  447.         else if ch in ['/','%','B'] then
  448.             if nmbrkling >= 5 then
  449.             ch := ' '
  450.         end;
  451.         subscreen[yscrn,xscrn] := ch;
  452.         yscrn := yscrn - 1
  453.     end;
  454.     xscrn  := xscrn + 1
  455.     end;
  456.     for ny := 10 downto 0 do begin
  457.     gotoxy(43,ny+1); write(subscreen[ny])
  458.     end
  459. end;
  460.  
  461. procedure condcheck {x,y : integer; var red : boolean};
  462. var nx,ny : integer;
  463. begin
  464.     babble := false;
  465.     red := false;
  466.     for nx := x - 10 to x + 10 do
  467.     for ny := y - 5 to y + 5 do
  468.         if (abs(nx) <= maxuni) and (abs(ny) <= maxuni) then
  469.         if universe[nx,ny].ch = 'T' then begin
  470.             babble := true; red := true
  471.         end
  472.         else if universe[nx,ny].ch in ['A','O','R','H','X','+'] then
  473.             red := true
  474. end;
  475.  
  476. procedure sector {x,y : integer; var result : integer};
  477. var
  478.     ch : char;
  479.     nx,ny,
  480.     klingons,
  481.     bases,
  482.     stars,
  483.     others : integer;
  484. begin
  485.     klingons := 0;
  486.     bases := 0;
  487.     stars := 0;
  488.     others := 0;
  489.     for nx := x - 10 to x + 10 do
  490.     for ny := y - 5 to y + 5 do
  491.         if (abs(nx) <= maxuni) and (abs(ny) <= maxuni) then begin
  492.         ch := universe[nx,ny].ch;
  493.         if ch = '+' then
  494.             klingons := klingons + 1
  495.         else if ch = '#' then
  496.             bases := bases + 1
  497.         else if ch = '*' then
  498.             stars := stars + 1
  499.         else if (ch <> ' ') and (ch <> '@') then
  500.             others := others + 1
  501.         end;
  502.     if others > 9 then
  503.     others := 9;
  504.     if stars > 9 then
  505.     stars := 9;
  506.     if bases > 9 then
  507.     bases := 9;
  508.     if klingons > 9 then
  509.     klingons := 9;
  510.     result := (((((others * 10) + stars) * 10) + bases) * 10) + klingons
  511. end;
  512.  
  513. procedure scrollup {up,down,left,right : integer};
  514. var
  515.     xdx,ydx,lngth : integer;
  516. begin
  517.     lngth := right - left;
  518.     for ydx := up to down - 1 do
  519.     move(screen[ydx + 1,left],screen[ydx,left],lngth);
  520.     move(blanks,screen[down,left],lngth);
  521.     for ydx := up to down do begin
  522.     gotoxy(left+1,ydx+1);
  523.     for xdx := left to left + lngth do
  524.         write(screen[ydx,xdx]);
  525.     end
  526. end;
  527.  
  528. procedure restore;
  529. var
  530.    fname : string80;
  531.    num: integer;
  532. begin
  533.     addln('What file? ');
  534.     togglecursor;
  535.     readln(con,fname);
  536.     togglecursor;
  537.     scrollinfo:= maxscroll;
  538.     assign(g,fname);
  539.     reset(g);
  540.     if ioresult = 0 then begin
  541.     blockread(g,msginfo[1],37*4);
  542.     clearscroll;
  543.     restored:= true;
  544.     close(g);
  545.     erase(g);
  546.     addscroll('Game retrieved. Continue.');
  547.     end
  548. end;
  549.  
  550. procedure driver;
  551. var
  552.    gstat: integer;
  553.    i : char;
  554. begin
  555.     { calling playgame with  gstat of 6 lets the player move first }
  556.     { calling playgame with gstat of 3 lets the enemy move first }
  557.     { all other gstats are set in playgame to control either:
  558.     a) the ending of the game (0..6)
  559.     b) start a new level      (-1)
  560.     c) calling restore        (-3)
  561.     d) calling one of the object handling routines in group (ord of cmd)
  562.     }
  563.     gstat := 6;
  564.     repeat
  565.     playgame(gstat);
  566.     if gstat = -1 then begin
  567.         clrmesg;
  568.         for i:= 'a' to currlst do
  569.         if list[i].ch = '/' then
  570.             return:= true;
  571.         if return then
  572.         level:= level - 1
  573.         else
  574.         level := level + 1;
  575.         if level > highest then
  576.         highest := level;
  577.         maxpower:= 3000 + points div 10 ;
  578.         if not return or (level <> 0) then begin
  579.         addscroll('       You are entering a');
  580.         if not return then
  581.             addln('       higher')
  582.         else
  583.             addln('       lower');
  584.         addscroll(' level.');
  585.         addscroll(' (Please wait)');
  586.         scroll;
  587.         startgame(-1); { creates  new universe }
  588.         clearscroll
  589.         end
  590.     end
  591.     else if gstat = -3 then
  592.         restore           { either loads a saved game or is NOP }
  593.     else if gstat in [0..7] then
  594.         startgame(gstat)  { isn't going to return }
  595.        else if gstat < 128 then
  596.         group(chr(gstat));
  597.     if gstat < 0 then
  598.         gstat := 6
  599.     else if chr(gstat) in ['D','d','G','g','U','u'] then
  600.         gstat := 3
  601.        else gstat := 6
  602.     until (level = 0) and return
  603. end;
  604.  
  605. begin (*main*)
  606.     clrscr;
  607.     alldone := false;
  608.     partdone := false;
  609.     fillchar(blanks,88,' ');
  610.     more := '--More --';
  611.     viscursor := true;
  612.     clrscr;
  613.     gotoxy(8,2);
  614.     writeln('   Stardate 2699.9, version II.13');
  615.     writeln;
  616.     writeln;
  617.     writeln('                 Transcript from Starfleet:');
  618.     writeln;
  619.     writeln('            Retrieve the Staff of Surak, which has been stolen');
  620.     writeln('         from its place on Vulcan.   Be wary of aliens.');
  621.     writeln;
  622.     writeln;
  623.     gotoxy(1,15); write('"restore" to resume a saved game');
  624.     gotoxy(1,16); write('"score" to see scores from previous games');
  625.     gotoxy(1,14); write('Enter your name, captain:  '); readln(con,captain);
  626.     if captain = 'score' then
  627.     startgame(8);
  628.     clrscr;
  629.     togglecursor;
  630.     level:= 1;
  631.     startgame(-2);
  632.     if alldone then goto 10;
  633.     restored:= false;
  634.     return:= false;
  635.     clearscroll;
  636.     if captain = 'restore' then
  637.        restore;
  638.     if alldone then goto 10;
  639.     if not restored then begin
  640.     startgame(-1);
  641.     if alldone then goto 10;
  642.     clearscroll;
  643.     addscroll('Your mission is ready to begin.');
  644.     end;
  645.     scroll;
  646.     driver;
  647.     clearscroll;
  648.     addscroll('Congratulations! You have');
  649.     addscroll(' returned from a dangerous');
  650.     addscroll(' mission. For your efforts, ');
  651.     addscroll(' you will receive a lifetime');
  652.     addscroll(' supply of C rations.');
  653.     delay(3000);
  654.     startgame(3);
  655. 10: end.
  656.  
  657.